package icu.bughub.app.app.ui.components

import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Divider
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.layout.layoutId
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.constraintlayout.compose.ConstraintLayout
import androidx.constraintlayout.compose.ConstraintSet
import androidx.constraintlayout.compose.Dimension
import coil.compose.AsyncImage
import icu.bughub.app.app.model.entity.VideoEntity

/**
 * @author huangzehui
 * @date 25/5/2024 下午4:50 周六
 * @description
 */

@Composable
fun VideoItem(videoEntity: VideoEntity) {

    val constraintSet = ConstraintSet {
        val covert = createRefFor("covert")
        val title = createRefFor("title")
        val type = createRefFor("type")
        val duration = createRefFor("duration")

        val divide = createRefFor("divide")

        // 设置约束
        constrain(covert) {
            start.linkTo(parent.start)
            top.linkTo(parent.top)
            centerVerticallyTo(parent)
            width = Dimension.value(115.5.dp)
        }

        constrain(title) {
            top.linkTo(covert.top)
            start.linkTo(covert.end, margin = 8.dp)
            end.linkTo(parent.end)
            horizontalBias = 0f
            width = Dimension.preferredWrapContent
        }


        constrain(type) {
            bottom.linkTo(covert.bottom)
            start.linkTo(title.start)
        }


        constrain(duration) {
            top.linkTo(type.top)
            start.linkTo(type.end, margin = 8.dp)
        }

        constrain(divide) {
            top.linkTo(covert.bottom, margin = 8.dp)
            start.linkTo(parent.start)
            end.linkTo(parent.end)
        }

    }


    ConstraintLayout(
        constraintSet = constraintSet,
        modifier = Modifier
            .fillMaxWidth()
            .padding(horizontal = 8.dp)
    ) {

        AsyncImage(
            model = videoEntity.imageUrl,
            contentDescription = null,
            contentScale = ContentScale.Crop,
            modifier = Modifier
                .layoutId("covert")
                .aspectRatio(16 / 9f)
                .clip(RoundedCornerShape(8.dp))
        )

        Text(
            text = videoEntity.title,
            fontSize = 16.sp,
            color = Color(0xFF666666),
            maxLines = 2,
            overflow = TextOverflow.Ellipsis,
            modifier = Modifier.layoutId("title")
        )
        Text(
            text = videoEntity.type,
            fontSize = 16.sp,
            color = Color(0xFF666666),
            maxLines = 1,
            overflow = TextOverflow.Ellipsis,
            modifier = Modifier.layoutId("type")
        )
        Text(
            text = "时长：${videoEntity.duration}",
            fontSize = 16.sp,
            color = Color(0xFF666666),
            maxLines = 1,
            overflow = TextOverflow.Ellipsis,
            modifier = Modifier.layoutId("duration")
        )


        Divider(modifier = Modifier.layoutId("divide"))
    }


}